
import { Notification, Message, Loading } from 'element-ui'

import { Commit, Dispatch } from 'vuex';
import { get, defaultsDeep, cloneDeep } from 'lodash'
import dayjs from 'dayjs'

import { getInspectionList, reviewInspection, getInspectionDetail } from '@/api/inspection'



export interface State {

}

const state: State = {
    search: {
        worker: '', //完工人
        dispatchNumber: '', //派工单号
        beginTime: '', //时间范围（开始）
        endTime: '',
        page: 1,
        pageSize: 20,
    },
    inspectionList: [],
    totalNumber: [],
    recordList: [],
    detail: {},
    inpsectionState: ''
};

const getters = {};

const mutations = {
    SET_SEARCH(state: any, payload: any) {
        state.search = payload
    },
    SET_INSPACTION_LIST(state: any, payload: any) {
        state.inspectionList = payload
    },
    SET_TOTAL(state: any, payload: any) {
        state.totalNumber = payload
    },
    SET_DETAIL(state: any, payload: any) {
        state.detail = payload
    },
    SET_RECORD_LIST(state: any, payload: any) {
        state.recordList = payload
    },
    setInspectionState(state: any, payload: any) {
        state.inpsectionState = payload
    },
};

const actions = {
    //表单数据更新
    COMMIT_TABLE_DATA({ commit, state }: any) {
        let params = state.search

        getInspectionList(params).then((resp) => {

            let inspectionList = get(resp, 'inspectionList', [])
            commit('SET_INSPACTION_LIST', inspectionList)
        })

    },

    //5-01查询质检记录 
    async GET_INSPECTION_LIST(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        commit('SET_SEARCH', payload)
        let resp = await getInspectionList(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            //   Message.success('请求成功!')
            console.log(resp);
            let inspectionList = get(resp, 'data', [])
            let totalNumber = get(resp, 'total', 0)
            console.log('is' + inspectionList)
            commit('SET_INSPACTION_LIST', inspectionList)
            commit('SET_TOTAL', totalNumber)
            return true
        }
    },
    //5-02查看质检记录详情
    async GET_INSPECTION_DETAIL(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        let resp = await getInspectionDetail(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            let detail = get(resp, 'data', {})
            commit('SET_DETAIL', detail)
            let recordList = get(detail, 'workRecordList', [])
            console.log(recordList);
            commit('SET_RECORD_LIST', recordList)
            // Message.success('请求详情成功!')

            return true
        }
    },
    //5-03质量审核  
    async REVIEW_INSPECTION(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        let resp = await reviewInspection(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            Message.success('提交成功!')
            //更新表格
            dispatch('COMMIT_TABLE_DATA')
            console.log(resp);
            return true
        }
    },
}


export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
};